package TestSparkConversionOperator.KeyValuePairRDD;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class SparkMapValues {
    public static void main(String[] args) {
        /**
         * 对键值对RDD的value进行处理，key保持不变，该算子返回一个新的键值对RDD。
         */
        SparkConf sparkConf = new SparkConf().setAppName("SparkMapValues").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);
        List<String> data = Arrays.asList("2021-10,47", "2021-10,47");
        JavaRDD<String> rdd1 = sc.parallelize(data);

        JavaPairRDD<String, Integer> pairRDD = rdd1.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String s) throws Exception {

                return new Tuple2<>(s.split(",")[0], Integer.parseInt(s.split(",")[1]));
            }
        });

        JavaPairRDD<String, Integer> newRdd = pairRDD.mapValues(new Function<Integer, Integer>() {
            @Override
            public Integer call(Integer integer) throws Exception {
                return integer + 10;
            }
        });
        System.out.println(newRdd.collect());

    }
}
